package com.example.demo.converter;

import com.example.demo.dao.Message;
import com.example.demo.dto.MessageDTO;

import java.util.HashSet;
import java.util.Set;

public class MessageConverter {
    public static MessageDTO convertMessage(Message message) {
        MessageDTO messageDTO = new MessageDTO();
        messageDTO.setId(message.getId());
        messageDTO.setMessageText(message.getMessageText());
        messageDTO.setReceiver(UserConverter.convertUser(message.getReceiver()));
        messageDTO.setSender(UserConverter.convertUser(message.getSender()));
        messageDTO.setSentAt(message.getSentAt());
        return messageDTO;
    }

    public static Message convertMessage(MessageDTO messageDTO) {
        Message message = new Message();
        message.setId(messageDTO.getId());
        message.setMessageText(messageDTO.getMessageText());
        message.setReceiver(UserConverter.convertUser(messageDTO.getReceiver()));
        message.setSender(UserConverter.convertUser(messageDTO.getSender()));
        message.setSentAt(messageDTO.getSentAt());
        return message;
    }

    public static Set<MessageDTO> convertToMessageDTOSet(Set<Message> messages) {
        Set<MessageDTO> messageDTOSet = new HashSet<>();
        for (Message message : messages) {
            messageDTOSet.add(convertMessage(message));
        }
        return messageDTOSet;
    }

    public static Set<Message> convertToMessageSet(Set<MessageDTO> messageDTOSet) {
        Set<Message> messages = new HashSet<>();
        for (MessageDTO messageDTO : messageDTOSet) {
            messages.add(convertMessage(messageDTO));
        }
        return messages;
    }
}
